$(() => {
    const token = GetQueryString("token");

    // 地址三级联动
    $('.cityList').on('click', function() {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'Api|get_area_list', 'pcode': 1 },
            dataType: 'json',
            success: function(res) {
                // // console.log(res);
                let data = res.data;
                let province = '';
                if (res.status == 1) {
                    data.forEach(item => {
                        province += `
                            <a href="javascript:;" data-code="${item.area_code}" class="h88 cityItem">${item.area_name}</a>
                         `;
                    })
                    $('.cityLists').html(province);
                    $(".cityItem").on('click', function() {
                        // // console.log($(this).attr('data-code'));
                        $('.province').val($(this).html());
                        let area_code = $(this).attr('data-code');
                        $('.province').attr('data-code', area_code);
                        $.ajax({
                            type: "post",
                            url: url,
                            data: { 'method': 'Api|get_area_list', 'pcode': area_code },
                            dataType: 'json',
                            success: function(res) {
                                // // console.log(res);
                                let data = res.data;
                                let city = '';
                                if (res.status == 1) {
                                    data.forEach(item => {
                                        city += `
                                        <a href="javascript:;" data-code="${item.area_code}" class="h88 cityItem">${item.area_name}</a>
                                         `;
                                    })
                                    $('.cityLists').html(city);
                                    $(".cityItem").on('click', function() {
                                        $('.city').val($(this).html());
                                        let area_code = $(this).attr('data-code');
                                        $('.city').attr('data-code', area_code);
                                        $.ajax({
                                            type: "post",
                                            url: url,
                                            data: { 'method': 'Api|get_area_list', 'pcode': area_code },
                                            dataType: 'json',
                                            success: function(res) {
                                                // // console.log(res);
                                                let data = res.data;
                                                let county = '';
                                                if (res.status == 1) {
                                                    data.forEach(item => {
                                                        county += `
                                                        <a href="javascript:;" data-code="${item.area_code}" class="h88 cityItem">${item.area_name}</a>
                                                         `;
                                                    })
                                                    $('.cityLists').html(county);
                                                    $('.cityItem').on('click', function() {
                                                        let area_code = $(this).attr('data-code');
                                                        $('.county').attr('data-code', area_code);
                                                        let province = $('.province').val();
                                                        let city = $('.city').val();
                                                        let conuty = $(this).html();
                                                        $('#ringadtext2').html(province + '-' + city + '-' + conuty);
                                                        dialogClose();
                                                    })
                                                }
                                            }
                                        })
                                    })
                                }
                            }
                        })
                    })
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    })

    // 发布类型二级联动
    $('.typelist').on('click', function() {
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'Forum|forum_category' },
            dataType: 'json',
            success: function(res) {
                // // console.log(res);
                let data = res.list;
                let html = '';
                if (res.status == 1) {
                    data.forEach(items => {
                        let arr = [];
                        items.son.forEach(son => {
                            arr.push([son.id, son.bname]);
                        })
                        html += `
                        <a href="javascript:;" data-list="${arr}" class="h88 typeItem">${items.bname}</a>
                    `;
                        $('.sendTypeLists').html(html);
                        $('.typeItem').on('click', function() {
                            let html = '';
                            let arr1 = $(this).attr('data-list');
                            arr1 = arr1.split(",");
                            for (let i = 0; i < arr1.length; i += 2) {
                                html += `
                                 <a href="javascript:;" class="h88 typeItem" data-ac_id="${arr1[i]}">${arr1[i+1]}</a>
                            `;
                            }
                            // items.son.forEach(item => {
                            //     html += `
                            //     <a href="javascript:;" class="h88 typeItem" data-ac_id="${item.id}">${item.bname}</a>
                            // `;
                            // })
                            $('.sendTypeLists').html(html);
                            $('.typeItem').on('click', function() {
                                let ac_id = $(this).attr('data-ac_id');
                                let bname = $(this).html();
                                $('#ringadtext1').attr('data-ac_id', ac_id);
                                $('#ringadtext1').html(bname);
                                dialogClose();
                            })
                        })
                    });
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    })

    $('.submit').on('click', function() {
        let arr1 = [],
            arr2 = [];
        let content = $('.radd_text').val();
        let ac_id = $('#ringadtext1').attr('data-ac_id');
        let province_code = $('.province').attr('data-code');
        let city_code = $('.city').attr('data-code');
        let country_code = $('.county').attr('data-code');
        for (var i = 0; i < $('.fileinput').length; i++) {
            let paths = $('.fileinput').eq(i).attr('data-path');
            let thumbs = $('.fileinput').eq(i).attr('data-thumb');
            arr1.push(paths);
            arr2.push(thumbs);
        }
        let imgs = arr1.join(',');
        let thumb_url = arr2.join(',');
        // console.log(paths);
        // console.log(province_code);
        if (content == "") {
            alert('内容不能为空！！！');
            return;
        } else if ($('.ring_pic_itme').length < 1) {
            alert('图片不能为空');
            return;
        } else if (ac_id == undefined) {
            alert('发布类型不能为空！！！');
            return;
        } else if (province_code == undefined) {
            alert('所在省份不能为空！！！');
            return;
        }
        $.ajax({
            type: "post",
            url: url,
            data: { 'method': 'Forum|forum_add', 'token': token, 'content': content, 'imgs': imgs, 'thumb_url': thumb_url, 'ac_id': ac_id, 'province_code': province_code, 'city_code': city_code, 'country_code': country_code },
            dataType: 'json',
            success: function(res) {
                if (res.status == 1) {
                    location.href = "fox://setBack";
                } else if (res.status == 2) {
                    location.href = "fox://logout";
                } else {
                    alert(res.message);
                }
            }
        })
    })
})